#!/bin/sh
#
# pfSense-rc.shutdown
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2004-2013 BSD Perimeter
# Copyright (c) 2013-2016 Electric Sheep Fencing
# Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if ! /usr/bin/lockf -s -t 30 /tmp/config.lock /usr/bin/true; then
	echo "Cannot shutdown at this moment, a config write operation is in progress and 30 seconds have passed."
	exit 1
fi

# Read product_label from $g, defaults to pfSense
product_label=$(/usr/local/sbin/read_global_var product_label pfSense)

echo
echo "${product_label} is now shutting down ..."
echo

/bin/stty status '^T'

# Set shell to ignore SIGINT (2), but not children;
trap : 2

HOME=/; export HOME
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

# Remove temporary files on shutdown from /tmp/
/usr/bin/find -x /tmp/* -type f -exec rm -f {} \; >/dev/null 2>&1

. /etc/rc.ramdisk_functions.sh
ramdisk_make_backup

# Drop out of all CARP vhids before interrupting services for clean HA transition
sysctl net.inet.carp.allow=0

# Invoke shutdown scripts if present
scripts=/usr/local/etc/rc.d/shutdown.*.sh
for script in $scripts
do
	if [ -f "$script" ] && [ -x "$script" ]
	then
		$script
	fi
done
